package com.ymm.utils.security;

import com.ymm.domain.entity.LoginUser;
import com.ymm.enums.SystemContains;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.List;

/**
 * @author 椰咩咩咩
 */
public class SecurityUtils {

    /**
     * 获取用户
     **/
    public static LoginUser getLoginUser() {
        return (LoginUser) getAuthentication().getPrincipal();
    }

    public static List<String> getPermissions() {
        return getLoginUser().getPermissions();
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public static Boolean isAdmin() {
        Long id = getLoginUser().getUser().getId();
        return id != null && SystemContains.SUPER_ADMIN_id.equals(id);
    }

    public static Long getUserId() {
        return getLoginUser().getUser().getId();
    }
}